<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <script src="js/jquery.min.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo2.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <!-- <button id="submit" onclick="add()">提交</button> -->
        <a href="blog_edit.html">写博客</a>
        <a href="javascript:onExit()">退出登陆</a>
        <!-- <a href="#">注销</a> -->
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 左侧个人信息 -->
        <div class="container-left">
            <div class="card">
                <img src="img/doge.jpg" class="avtar" alt="">
                <h3 id="username">比特小可爱</h3>
                <a href="http:www.github.com">github 地址</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span>2</span>
                    <span>1</span>
                </div>
            </div>
        </div>
        <!-- 右侧内容详情 -->
        <div class="container-right" id="artlistDiv">
            <!-- 每一篇博客包含标题, 摘要, 时间 -->
            <div class="blog">
                <div class="title">我的第一篇博客</div>
                <div class="date">2021-06-02</div>
                <div class="desc">
                    从今天起, 我要认真敲代码. Lorem ipsum, dolor sit amet consectetur adipisicing elit. Cum distinctio ullam eum ut
                    veroab laborum numquam tenetur est in dolorum a sint, assumenda adipisci similique quaerat vel.
                    Facere,
                    et.
                </div>
                <a href="blog_content.html?id=1" class="detail">查看全文 &gt;&gt;</a>
            </div>
            <div class="blog">
                <div class="title">我的第二篇博客</div>
                <div class="date">2021-06-02</div>
                <div class="desc">
                    从今天起, 我要认真敲代码. Lorem ipsum, dolor sit amet consectetur adipisicing elit. Cum distinctio ullam eum ut
                    veroab laborum numquam tenetur est in dolorum a sint, assumenda adipisci similique quaerat vel.
                    Facere,
                    et.
                </div>
                <a href="blog_content.html?id=2" class="detail">查看全文 &gt;&gt;</a>
            </div>
        </div>
    </div>
</body>
<script>
    //截取字符串
    function mySubstr(content){
        if(content.length>50){
            return content.substr(0,50);
        }
        return content;
    }
    function initList(){
            jQuery.ajax({
                url:"/art/mylist",
                type:"POST",
                data:{},
                success:function(result){
                    // console.dir(result);
                    if(result.code==200 && result.data!=null&&result.data.length>0){
                        var html="";
                        result.data.forEach(function(item){
                            html+='<div class="blog">'+
                                '<div class="title">'+item.title+'</div>'
                                +'<div class="date">'+item.create_time+'</div>'+'<div class="desc">'
                                    +mySubstr(item.content)+'</div>'+
                                    '<div style="text-align: center;margin-top :50px;">'+
                                    '<a href="blog_content.html?id='+item.id+'"> 查看详情 </a>&nbsp;&nbsp;'+
                                    '<a href="blog_update.html?id='+item.id+
                                    '">修改</a>&nbsp;&nbsp;<a href="javascript:myDel('+item.id
                                    +')">删除</a></div>'
                                    +'</div>';
                        });
                        jQuery("#artlistDiv").html(html);
                    }else{
                        //未发表数据
                        jQuery("#artlistDiv").html("<h1>暂无数据</h1>")
                    }
                },
                error:function(err){
                    if(err!=null&&err.status==401){
                        alert("用户未登录,即将跳转到登陆页");
                        location.href= "/login.html";
                    }
                }
            });
    }
    initList();
    //获取个人信息
    function myinfo(){
        jQuery.ajax({
            url:"/user/myinfo",
                type:"POST",
                data:{},
                success:function(result){
                    if(result.code==200&&result.data!=null){
                        jQuery("#username").text(result.data.username);

                    }
                },
                error:function(err){
                    
                }
        });
    }
    myinfo();
    function onExit(){
        if(confirm("确认退出？")){
            jQuery.ajax({
                url:"/user/logout",
                type:"POST",
                data:{},
                success:function(result){
                    // alert(JSON.stringify(result));
                    location.href ="/login.html";
                },
                error:function(err){
                    alert(JSON.stringify(err));
                    if(err!=null&&err.status==401){
                        // alert(JSON.stringify(err));
                        // alert("用户未登录,即将跳转到登陆页");
                        location.href= "/login.html";
                    }
                }
            })
        }
    }
    function myDel(id){
        if(confirm("确认删除？")){
            jQuery.ajax({
                url:"/art/mydel",
                type:"POST",
                data:{
                    "id":id
                },
                success:function(result){
                    alert("删除成功！");
                    location.href="/myblog_list.html";
                },
                error:function(err){
                    alert("未删除成功！");
                    if(err!=null&&err.status==401){
                        // alert(JSON.stringify(err));
                        // alert("用户未登录,即将跳转到登陆页");
                        location.href= "/login.html";
                    }
                }
            })
        }
    }
</script>
</html>